package plot
{
	import mx.collections.ArrayCollection;
	import mx.managers.CursorManager;
	
	public class DataManager
	{
		[Bindable]
		public function set labels(value:ArrayCollection):void
		{
			_labels = value;
			updateData();
		}
		private var _labels:ArrayCollection;
		
		[Bindable]
		public function get dataProvider():ArrayCollection
		{
			return _dataProvider;
		}
		public function set dataProvider(value:ArrayCollection):void
		{
			_dataProvider = value;
			updateData();
		}
		private var _dataProvider:ArrayCollection;
		
		[Bindable]
		public var processedData:Object;
		
		[Bindable]
		internal var minimumValues:Array = [];
		
		[Bindable]
		internal var maximumValues:Array = []
		
		private var scaleIndex:Array = [];
		
		public function DataManager()
		{
		}
		
		private function updateData():void
		{
			
		}
		/**
        * Organizes categorical data and dates (sorts them, assigns
        * numerical values for later scaling) 
        */
        private function preprocessCustomDataTypes():ArrayCollection
        {
        	trace("processing " + new Date().toString());
        	
        	CursorManager.setBusyCursor();
        	// add all columns that contain Strings or Dates
        	var customDataIndex:ArrayCollection = new ArrayCollection();
        	var dictionary:Array = new Array();
        	var map:Object;
        	var label:String;
        	var j:int;
        	
        	minimumValues = new Array();
        	maximumValues = new Array();
        	
        	var column_count:int = _labels.length;
        	var row_count:int = dataProvider.length;
        	
        	for (var i:int = 0; i < column_count; i++)
        	{
        		label = _labels[i];
        		maximumValues[label] = dataProvider[0][label];
	        	minimumValues[label] = dataProvider[0][label];
        		
        		/// if date
        		if (dataProvider[0][label] is Date )
        		{
        			customDataIndex.addItem(label);
        			dictionary[label] = {};
        			// go through all rows in this column and add all of the unique values to the map
        			for (j = 0; j < row_count; j++)
        			{
        				map = dictionary[label];
        				map[dataProvider[j][label]] = dataProvider[j][label];
        			}
        			dictionary[label] = Utils.sortMap(dictionary[label], Date);
        		}
        		// if string
        		else if (dataProvider[0][label] is String)
        		{
        			customDataIndex.addItem(label);
        			dictionary[label] = {};
        			// go through all rows in this column and add all of the unique values to the map
        			for (j = 0; j < row_count; j++)
        			{
        				map = dictionary[label];
        				map[dataProvider[j][label]] = dataProvider[j][label];
        			}
        			dictionary[label] = Utils.sortMap(dictionary[label], String);
        		}
        	}
        		
        	var new_data:ArrayCollection = new ArrayCollection();
        	for (i = 0; i < row_count; i++)
        	{
        		new_data.addItem( new Array());
        	}
        	
    		var _min:Object;
    		var _max:Object;
    		// copy data for the number columns
    		for (j = 0; j < column_count; j++)
    		{
    			label = _labels[j];
    			_min = minimumValues[label];
    			_max = maximumValues[label];
    		
    			if (!customDataIndex.contains(_labels[j]))
    				for (i = 0; i < row_count; i++)
    				{
    					new_data[i][label] = dataProvider[i][label];
    				
    				if (dataProvider[i][label] < _min) 
        				_min = dataProvider[i][label];
        			if (dataProvider[i][label] > _max)
        				_max = dataProvider[i][label];
    			}
        		minimumValues[label] = _min;
        		maximumValues[label] = _max;
        	}
        			
        	// substitute values with numbers
        	for (j = 0; j < customDataIndex.length; j++)
        	{
        		label = customDataIndex[j];
        		map = dictionary[label];
        		_min = minimumValues[label] = map[dataProvider[j][label]];
        		_max = maximumValues[label] = map[dataProvider[j][label]]
        		
        		for (i = 0; i < row_count; i++){
        			var value:String = dataProvider[i][label].toString();
        			new_data[i][label] = map[value];
        			
        			if (new_data[i][label] < _min) 
	        			_min = new_data[i][label];
	        		if (new_data[i][label] > _max)
	        			_max = new_data[i][label];
        		}
        		minimumValues[label] = _min;
        		maximumValues[label] = _max;
        			
        	}
        	CursorManager.removeBusyCursor();
        	return new_data;
        }
//		
//		private function calculateScaleIndex():ArrayCollection
//        {
//        	trace("scaling " + new Date().toString());
//        	// TODO: fill out
//        	var scaledData:ArrayCollection;
//        	
//
//        	scaleIndex = new Array();
//        	var label:String;
//        	
//        	var column_count:int = _labels.length;
//        	
//        	for (var i:int = 0; i < column_count ; i++)
//        	{
//        		label = _labels[i];
//        		if (max[label] - min[label] == 0)
//        			if (max[label] == 0)	// all values are 0's
//        				scaleIndex[i] = this.height;
//        			else
//        				scaleIndex[i] = this.height/max[label];	// scale down
//        		else 
//        			scaleIndex[i] = (this.height - 2 * borderY) /(max[label] - min[label]);
//        	}
//        	
//        	return scaledData;
//        }
	}
}